"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库maketime函数
Case Name   : maketime函数参数取值范围测试
Create At   : 2022.11.25
Owner       : z@wan005
Description :
    1、hour第一个参数测试
    2、minute第二个参数测试
    3、second第三个参数测试
    4、第三个参数带微秒
    5、微秒超过6位
    6、时分秒均为0
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_maketime_0001"

    def test_utc_date(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:hour第一个参数测试;expect:成功--'
        self.log.info(text)
        sql_cmd = "select maketime(-838,0,0);" \
                  "select maketime(838,0,0);" \
                  "select maketime('838',0,0);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual('-838:00:00', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('838:00:00', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('838:00:00', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step2:minute第二个参数测试;expect:成功--'
        self.log.info(text)
        sql_cmd = "select maketime(838,0,59);" \
                  "select maketime(838,59,59);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 9, '行数不到9行')
        self.assertEqual('838:00:59', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('838:59:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step3:second第三个参数测试;expect:成功--'
        self.log.info(text)
        sql_cmd = "select maketime(838,0,0);" \
                  "select maketime(838,'25',59);" \
                  "select maketime(0,0,1000000000000000000);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual('838:00:00', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('838:25:59', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual(0, len(sql_res.splitlines()[-2].strip()),
                         '执行失败' + text)

        text = '--step4:第三个参数带微秒;expect:成功--'
        self.log.info(text)
        sql_cmd = "select maketime(24,59,59.9);" \
                  "select maketime(24,59,59.99);" \
                  "select maketime(24,59,59.999);" \
                  "select maketime(24,59,59.9999);" \
                  "select maketime(24,59,59.99999);" \
                  "select maketime(24,59,59.999999);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 29, '行数不到29行')
        self.assertEqual('24:59:59.9', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.99', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.999', sql_res.splitlines()[12].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.9999', sql_res.splitlines()[17].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.99999', sql_res.splitlines()[-7].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.999999', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step5:微秒超过6位;expect:成功--'
        self.log.info(text)
        sql_cmd = "select maketime(24,59,59.9999999);" \
                  "select maketime(24,59,59.9999991);" \
                  "select MAKETIME(0, 10, 59.999999500);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        msg = sql_res.splitlines()
        self.log.info(msg)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual('25:00:00', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.999999', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('00:11:00', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step6:时分秒均为0;expect:成功--'
        self.log.info(text)
        sql_cmd = "select maketime(0,0,0);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual('00:00:00', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '----step7:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
